/**
 * 
 */
var homeworkModel = {
	htype: 1,
	xzb: [],
	content: null,
	icon: null,
	exampk: null,
	attachment: [],
	starttime: null,
	endtime: null,
	videopk: null,
	timepoint: [],
	classname: '',
	cname: '',
	creator: null
}

function resetModel() {
	homeworkModel = {
		htype: 1,
		xzb: [],
		content: null,
		icon: null,
		exampk: null,
		attachment: [],
		starttime: null,
		endtime: null,
		videopk: null,
		timepoint: [],
		classname: '',
		cname: '',
		creator: null
	}
}

var loadi = {
	video: null,
	file: null,
	img: null
}
jQuery(document).ready(function(){
	jQuery('#startTime').datepicker();
	jQuery('#endTime').datepicker();

	var acac = '2015_2016_2';
	var uid = getCookies(COOKIE_NAME);
	jQuery.get('web/courselist',{academic:acac,uid:uid},function(data,status){
			initCourseList(data);
	},"json");

	jQuery.get('web/myexampk',{uid:uid},function(data,status){
		initExamPkList(data);
	},"json");

	jQuery('input[type="radio"][name="hmiconradio"]').change(function () {
		if (jQuery(this).val() == '10') {
			jQuery('#iconinputDiv').hide();
		}
		else {
			jQuery('#iconinputDiv').show();
		}
	});
	
	jQuery('#addOneAttachmentBtn').on('click',function () {
		// jQuery('#attachmentDiv').append('<br/><div class="uploader"><input type="file" name="attachment[]" size="19" style="opacity: 0;"><span class="filename">No file selected</span> <span class="action">Choose File</span></div> <button class="stdbtn btn_orange removeinput" type="button">-</button>');
		jQuery('#attachmentDiv').append('<div><input type="file" name="filename[]" class="attachmentupload"/>&nbsp;<button class="stdbtn btn_orange removeInput" type="button"><span style="font-size:15px;height:0.7em;width:0.7em;display:table-caption">-</span></button></div>');
		jQuery('#attachmentDiv').trigger( "create" );
		jQuery('.attachmentupload').uniform();
		initNewAttach();
	});

	initBindData();

});

function initNewAttach() {
	jQuery('.removeInput').on('click',function () {
		jQuery(this).parent().remove();
	});
}

function initCourseList(data){
	var size = data.length;
	for(var i =0;i<size;i++){
		jQuery('#courseTab').append('<li><button class="stdbtn courseTab" autoid="'+data[i].id+'" dataid="'+data[i].cid+'" onclick="tabClickCss(jQuery(this))">'+data[i].name+'</button></li>');
	}
}

function tabClickCss(e){
	if(jQuery(e).hasClass('btn_orange')){
		jQuery(e).removeClass('btn_orange');
		if(jQuery(e).hasClass('courseTab')){
			var cid = jQuery(e).attr('dataid');
			jQuery('#'+cid).remove();
			jQuery(e).parent().find('b').remove();
		}
	}
	else{
		jQuery(e).addClass('btn_orange');
		if(jQuery(e).hasClass('courseTab')){
			var academic='2015_2016_2';
			var cid = jQuery(e).attr('dataid');
			var uid = getCookies(COOKIE_NAME);
			jQuery.post('web/classlist',{academic:academic,cid:cid,uid:uid},function(data,status){
				initClassTabList(data,cid,jQuery(e));
			},"json");
		}
	}
}

function initClassTabList(data,id,e){
	var liIdx = jQuery(e).parent().index()+1; 
	jQuery(e).parent().append('<b class="circularDiv">'+liIdx+'</b>');
	jQuery('#xzbTabDiv').append('<ul class="buttonlist" id="'+id+'"></ul>');
	var tempdiv = jQuery('#'+id);
	jQuery(tempdiv).append('<b class="circularDiv2">'+liIdx+'</b>');
	var size = data.length;
	for(var i =0;i<size;i++){
		jQuery(tempdiv).append('<li><button class="stdbtn classTab" srcid="'+data[i].id+'" dataid="'+data[i].code+'" onclick="tabClickCss(jQuery(this))">'+data[i].xzb+'</button></li>');
	}
}

function initExamPkList(data) {

	if(data.code == '0000') {
		jQuery(data.data).each(function(idx,item){
			jQuery('#exampk').append('<option value="'+item.pk+'">'+item.content+'</option> ');
		});

	}
}

function initBindData() {
	jQuery('#sumbitHw').on('click',function () {
		var xzbList = jQuery('#xzbTabDiv').children('ul');
		jQuery(xzbList).each(function (aidx, aitem) {
			var xzbliList = jQuery(aitem).children('li').children('button');
			jQuery(xzbliList).each(function (bidx, bitem) {
				if (jQuery(bitem).hasClass('btn_orange')) {
					// alert(jQuery(bitem).attr('dataid'));
					homeworkModel.xzb.push(jQuery(bitem).attr('srcid'));
				}
			});
		});
		// homeworkModel.xzb.length = 0;

		jQuery(jQuery('.tag')).each(function (idx,item) {
			var t = jQuery(item).children('span').text();

			if (isNaN(t)) {
				layer.tips("必须正整数",item,1);
			}
			else {
				homeworkModel.timepoint.push(t);
			}
		});
		// homeworkModel.timePoint.length = 0;

		homeworkModel.htype = jQuery('#htype').val();
		homeworkModel.content = jQuery('#hname').val();
		homeworkModel.exampk = jQuery('#exampk').val();
		jQuery(jQuery('#courseTab').find('.btn_orange')).each(function (i,item) {
			homeworkModel.cname = homeworkModel.cname + jQuery(item).text() + ',';
		});
		if (homeworkModel.cname.length != 0) {
			homeworkModel.cname = homeworkModel.cname.substring(0,homeworkModel.cname.length - 1);
		}
		homeworkModel.creator = jQuery('#sysuid').text();
		uploadImg();

		/**
		 * 组装日期
		 */
		homeworkModel.starttime = stringToDate(jQuery('#startTime').val()).valueOf()/1000;
		homeworkModel.endtime = stringToDate(jQuery('#endTime').val()).valueOf()/1000;
	});
}

function uploadImg() {
	loadi.img = layer.msg('正在上传作业图片', {
		icon: 16
		,shade: 0.01
	});
	uploadfile(jQuery('#iconinputDiv').parent(),'homework','img',function (result) {
		homeworkModel.icon = result[0];
		layer.close(loadi.img);
		uploadFile();
	});
}

function uploadFile() {
	loadi.file = layer.msg('正在上传附件资料', {
		icon: 16
		,shade: 0.01
	});
	uploadfile(jQuery('#attachmentDiv'),'homework','file',function (result) {
		homeworkModel.attachment = result;
		layer.close(loadi.file);
		uploadVideo();
	});
}

function uploadVideo() {
	loadi.video = layer.msg('正在上传视频文件', {
		icon: 16
		,shade: 0.01
	});
	uploadfile(jQuery('#videoDiv'),'homework','video',function (result) {
		homeworkModel.videopk = result[0];
		layer.close(loadi.video);
		formCommit();//正式提交数据
	});
}

function formCommit() {
	// var t = {exampk:homeworkModel.exampk};
	jQuery.ajax({
		type: 'POST',
		url: 'web/submithw',
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify(homeworkModel),
		success: function (data) {

		},
		error: function (data) {
			resetModel();
		},
		dataType: "json"
	});
}

/**
 * 上传文件
 */
function uploadfile(formthis, uptype, filetype, callback){
	jQuery(formthis).ajaxSubmit({
		type: "POST",
		url: "http://115.28.164.193/upload.php?upfor="+uptype+"&filetype="+filetype,
		dataType: "json",
		// headers: {"token": sessionStorage.getItem("token")},
		success: function(data){
			var callresult = [];
			if (data instanceof Array) {
				jQuery(data).each(function(i, item){
					callresult.push(item);
				});
			}
			else {
				callresult.push(data);
			}
			callback(callresult);
		}
	});
}

function getObjectURL(file) {
	var url = null ; 
	if (window.createObjectURL!=undefined) { // basic
		url = window.createObjectURL(file) ;
	} else if (window.URL!=undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file) ;
	} else if (window.webkitURL!=undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}
